package com.itboye.ihomebank.key;

import java.util.Observable;
import java.util.Observer;

import org.greenrobot.greendao.database.Database;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.itboye.ihomebank.KeyFragment;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.DownBeifenBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.ttKey.DaoMaster;
import com.itboye.ihomebank.ttKey.DaoSession;
import com.itboye.ihomebank.ttKey.DbService;
import com.itboye.ihomebank.ttKey.Key;
import com.itboye.ihomebank.ttKey.KeyDao;
import com.itboye.ihomebank.ttKey.MyPreference;
import com.itboye.ihomebank.ttKey.Operation;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 备份钥匙的管理员用户（锁）
 * 
 */
public class ActivityGuanLiKeyTwo extends BaseOtherActivity  implements Observer{
	ImageView key_finish;// 返回
	TextView key_title;// 标题
	ImageView key_shezhi;// 设置
	ImageButton icon;// 开锁
	// 发送钥匙，发送密码，用户管理，开锁记录
	LinearLayout key_send_yaoshi, key_send_pass, key_guanli, key_jilu;
	public static String lockid;
	private String keyid;
	private String lockMac;
	private String lockAlias;
	LinearLayout bottom;
	String uid;

	public static boolean addLock = false;
	
	public static BluetoothDevice device;
	
	KeyPresenter keyPresenter;
	
	public static DownBeifenBean bean;
	
	BluetoothAdapter mBluetoothAdapter;
	
	public static KeyDao keyDao;
	private static DaoSession daoSession;
	
	Gson gson = new Gson();

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.key_finish:
			finish();
			break;
		case R.id.key_shezhi:// 设置
			intent = new Intent(this, ActivityKeySheZhiTwo.class);
			intent.putExtra("lockid", lockid);
			intent.putExtra("keyid", keyid);
			intent.putExtra("lockAlias", lockAlias);
			startActivity(intent);

			break;
		case R.id.key_send_yaoshi:// 发送钥匙
			intent = new Intent(this, ActivitySendKey.class);
			intent.putExtra("lockid", lockid);
			startActivity(intent);
			break;
		case R.id.key_send_pass:// 发送密码
			intent = new Intent(this, ActivityFaSongPass.class);
			intent.putExtra("lockid", lockid);
			startActivity(intent);
			break;
		case R.id.key_guanli:// 用户管理
			intent = new Intent(this, ActivityYongHuGuanLi.class);
			intent.putExtra("lockid", lockid);
			startActivity(intent);
			break;
		case R.id.key_jilu:// 开锁记录
			intent = new Intent(this, ActivityKaiSuoJiLuOne.class);
			intent.putExtra("keyid", keyid);
			intent.putExtra("lockid", lockid);
			startActivity(intent);
			break;
		case R.id.icon://开锁
			addLock = true;
			KeyFragment.bleSession.setOperation(Operation.UNLOCK);
			KeyFragment.bleSession.setLockmac(lockMac);

			break;
		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		return R.layout.activity_guanli_key;
	}

	@Override
	protected void onInitController() {
		mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
		
		DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(
				this, "zhujia.db", null);
		Database db = helper.getWritableDb();
		daoSession = new DaoMaster(db).newSession();
		keyDao = daoSession.getKeyDao();

	}

	@Override
	protected void onInit() {
		keyid = getIntent().getStringExtra("keyid");
		lockid= getIntent().getStringExtra("lockid");
		lockMac= getIntent().getStringExtra("lockMac");
		lockAlias= getIntent().getStringExtra("lockAlias");
		key_title.setText(lockAlias);
		keyPresenter=new KeyPresenter(this);
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		
		
		//判断钥匙是否本地存在，存在则无需下载
		String accessToken = MyPreference.getStr(mContext,
				MyPreference.ACCESS_TOKEN);
		Key localKey = DbService.getKeyByAccessTokenAndLockmac(accessToken,
				lockMac);
		if(localKey!=null){
			//ByAlert.alert("钥匙已存在，无需下载");
		}else{
			showProgressDialog("数据加载中,请稍后", true);
			keyPresenter.getDownKeyBeiFen(uid, lockid, keyid);
		}

	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (entity != null) {
			if (entity.getEventType() == KeyPresenter.downBeifen_success) {
				ByAlert.alert(entity.getData());
				bean=(DownBeifenBean) entity.getData();
				
				final Key key = new Key();
				key.setAccessToken(MyPreference.getStr(mContext,
						MyPreference.ACCESS_TOKEN));
				key.setAdmin(true);
				key.setLockVersion(gson.toJson(bean.getLockVersion()));
				key.setLockName(bean.getLockName());
				key.setLockMac(bean.getLockMac());
				key.setAdminPs(bean.getAdminPs());
				key.setUnlockKey(bean.getLockKey());
				key.setAdminKeyboardPwd(bean.getNokeyPs());
				key.setDeletePwd(bean.getDeletePs());
				key.setPwdInfo("");
				key.setTimestamp(0);
				key.setAesKeystr(bean.getAesKeyStr());
				key.setId(keyDao.insert(key));
				
			}	
			if (entity.getEventType() == KeyPresenter.downBeifen_fail) {
				ByAlert.alert(entity.getData());
				
			}	
		}
	}

}
